// pages/home/pages/activity/activity.js
import {http} from '../../../../utils/apiutils'
import {input} from '../../../../utils/tools'
import {formatTime} from '../../../../utils/util'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isCanDraw:false,
		obj:{},
		bgImg:'http://linghuoimage.xbbzp.com/FgEn_LXZ8GgEqrlWT7JWh9Tl2jj1',
		code_img:'',
		phone:'',
		username:'',
		orderNo:'',
		eventId:'',
		getList:[],
		openid:'',
		zig:false,
		page:1,
		pageSize:20,
		loading:false,
		hasMore:true,
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			eventId:options.id,
			username:getApp().globalData.userInfo.username,
			openid:getApp().globalData.userInfo.openid
		})
		this.getInfo()
		this.getGiftList()
		this.getCode()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow () {
		this.checkzige()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},
	createShareImage() {
		this.setData({
			isCanDraw: !this.data.isCanDraw
		})
	},
	getInfo(){
        http.eventInfo(this.data.eventId,res=>{
            if(res.code==0){
                // let obj = {
                //     width:res.data.width&&res.data.width+'px',
                //     height:res.data.high&&res.data.high+'px',
                //     left:res.data.marginLeft&&res.data.marginLeft+'px',
                //     bottom:res.data.marginBottom&&res.data.marginBottom+'px',
                //     code_width:res.data.qrWidth&&res.data.qrWidth+'px',
                //     code_height:res.data.qrHigh&&res.data.qrHigh+'px',
				// 	btn_background:res.data.buttonColor,
				// }
                let obj = {
                    width:750+'rpx',
                    height:'1191rpx',
                    left:450+'rpx',
                    bottom:100+'rpx',
                    code_width:200+'rpx',
                    code_height:200+'rpx',
					btn_background:res.data.buttonColor,
				}
                this.setData({
                    obj
                })
            }else{
                wx.showToast({
                    title: res.msg,
                    icon:"none"
                })
            }
        },err=>{
            wx.showToast({
              title: res.msg,
              icon:"none"
            })
        })
	},
	input:function(e){
		input.bind(this)(e)
	},
	saveOrUpdate(e){
		let params = {
			eventId:this.data.eventId,
			username:this.data.username,
			phone:this.data.phone,
			orderNo:this.data.orderNo,
			openid:this.data.openid,
			unionid:getApp().globalData.userInfo.unionid
		}
		if(!this.data.zig){
			wx.showToast({
			  title: '请先获取资格',
			  icon:"none"
			})
			return
		}
		if(e.currentTarget.dataset.key==1&&!this.data.phone){
			wx.showToast({
				title: '您已获取资格',
				icon:"none"
			  })
			  return
			return
		}
		if(e.currentTarget.dataset.key==1&&this.data.zig){
			return
		}
		if(e.currentTarget.dataset.key==2&&!this.data.orderNo) return ;
		http.saveOrUpdate(params,res=>{
			if(e.currentTarget.dataset.key==2){
				this.getGiftList()
			}
			this.checkzige()
			wx.showToast({
			  title: res.msg,
			  icon:"none"
			})
		},err=>{console.log(err)})
	},
	getGiftList(){
		if(this.data.loading||!this.data.hasMore) return ;
		this.data.loading = true;
		let params={
			id:this.data.eventId,
			page:this.data.page,
			pageSize:this.data.pageSize
		}
		http.getGiftList(params,res=>{
			if(res.obj.records.length==0){
				this.data.hasMore = false;
				return
			}
			this.data.page +=1;
			let arr = res.obj.records.filter(item=>{
				item.createTime = formatTime(item.createTime)
				return item
			})
			this.setData({
				loading:false,
				getList:[...this.data.getList,...arr]
			})
		},err=>{})
	},
	checkzige(){
		let params = {
			eventId:this.data.eventId,
			openid:this.data.openid,
			unionid:getApp().globalData.userInfo.unionid
		}
		http.checkzige(params,res=>{
			if(res.status==200){
				this.data.zig = true;
			}
		},err=>{})
	},
	getCode(){
		let params={
			path:'pages/index/index',
			param:'route=activity&id='+this.data.eventId
		}
		http.createNewCode(params,res=>{
			this.setData({
				code_img:'data:image/png;base64,'+res
			})
		},err=>{})
	}
})